/**
 * 登录地址
 */
//var loginUrl="http://www.treenity.com/login";
//var newLoginUrl = "http://treenity.zhihuishu.com/login";
var requestLoginUrl = "https://passport.zhihuishu.com/login";
var newUsername="";
$(function(){
	//当用户输入http://www.treenity.com/login(已登录的情况下)
	//跳转至http://treenity.zhihuishu.com/login
	/*var currLocation= window.location;
	if(loginUrl == currLocation){
		window.location.href=newLoginUrl;
	}*/
	login();
});

/**
 * 校验用户名
 * 只能以字母或数字开头，可带._@
 */
function checkUserName(obj){
	var username = $(obj).val();
	$(obj).val(username.replace())
	if ($.trim(username) == "" || username == null) {
		newUsername=username;
		return $(obj).val(username);
	}
	var reg=/^[a-zA-Z0-9][a-zA-Z0-9_.@-]*$/;
	if(!reg.test(username)){
		username = newUsername;
	}else{
		newUsername=username;
	}
	return $(obj).val(username);
}

/**
 * 登录方法
 */
function login(){
	$("#loginBtn").one("click",function(){
		//对用户名进行校验
		var usernameFlag = validateUsername();
		if(!usernameFlag){
			login();
			return false;
		}
		//对密码进行校验
		var passwordFlag = validateLPassword();
		if(!passwordFlag){
			login();
			return false;
		}
		
		//获取用户名密码
		var username = $("#username").val();
		var password = $("#password").val();
		//设置跳转页面
		var url =requestLoginUrl;
		//调用登录方法
		loginZHS(username,password,url);
	});
}

/**
 * 验证登录用户名
 */
validateUsername = function(){
	var username = $("#username").val();
	//非空验证
	if ($.trim(username) == "" || username == null) {
		addError("loginErrorSpan",zLocale.login_yonghumingbunengweikong);
		return false;
	}
	return true;
};

/**
 * 验证登录密码
 */
validateLPassword = function(){
	var password = $("#password").val();
	if ($.trim(password) == "" || password == null) {
		addError("loginErrorSpan",zLocale.login_mimabunengweikong);
		return false;
	}else{
		
		return true;
	}
};

loginZHS = function (username,password,url){

	var params = {
		account : username,
		password : password,
		url : url
	};
	$.ajax( {
		url : basePath+"/login/login",
		type : "POST",
		dataType :"json",
		data : params,
		success : function(data) {
			if(data !=null && data !="null" && data !="undefined"){
				if(url == null || url == "" || url == "undefined"){
				}else{
					//https://passport.zhihuishu.com/login?pwd=一次性密码&service=登录成功后跳转地址<br>
					url+=("?pwd="+data.password+"&service=http://www.treenity.com/course/home");
					window.location.href = url;
					
				}
			}else{
				addError("loginErrorSpan",zLocale.login_yonghuminghuomimacuowu);
				login();
				return false;
			}
		},
		error : function() {
			addError("loginErrorSpan",zLocale.login_yonghuminghuomimacuowu);
			login();
			return false;
		}
	});
};
/**
 * 添加错误提示
 * @param id1	输出框后div的id
 * @param id2	错误显示div的id
 * @param info	错误信息
 */
function addError(id1, info) {
	var obj = $("#" + id1);
	obj.css("display","block");
	obj.html(info);
}
/**
 * 弹窗提示
 * @param info
 */
function alertAddError(info){
	alert(info);
}

/**
 * 按下键盘事件
 * 当按下回车时，实现登录
 */	
$(window).keydown(function(event){
  if (event.keyCode==13){    
	$("#loginBtn").click();
  }  
});